#ifndef DOWNLOADER_LOGIC_H
#define DOWNLOADER_LOGIC_H

#include <string>
#include <vector>
#include <thread>
#include <mutex>
#include <atomic>
#include <fstream>
#include <glibmm/dispatcher.h>
#include "downloader_window.h" // Include window header for UIData struct

// Forward declare curl types
typedef void CURL;

// Structure for download block info (similar to original)
struct DownloadBlock {
    long long start;
    long long end;
    std::string url;
    std::string temp_filename;
    int block_id;
};

class DownloaderLogic {
public:
    // Constructor takes shared resources via references
    DownloaderLogic(std::string url, std::string savePath, int numThreads,
                     std::atomic<bool>& cancelFlag,
                     Glib::Dispatcher& dispatcher,
                     DownloaderWindow::UIData& uiData, // Reference to shared data
                     std::mutex& uiDataMutex); // Reference to mutex

    ~DownloaderLogic(); // Destructor for potential cleanup

    // Main function executed by the dedicated thread
    void run_download();

private:
    // Helper methods adapting original logic
    long long get_file_size_internal();
    void prepare_download_blocks();
    void launch_worker_threads();
    void download_block_worker(DownloadBlock blockInfo); // Function run by each worker thread
    bool merge_files_internal();

    // Reporting methods to update shared UI data safely
    void report_progress_increment(long long downloaded_increment);
    void report_final_progress(); // Update total downloaded size at the end
    void report_status(const std::string& message, bool is_error = false, bool is_finished = false, const std::string& error_details = "");

    // --- Member Variables ---
    std::string m_url;
    std::string m_savePath;
    std::string m_baseTempFilename; // Base name for temp files
    int m_numThreads;
    long long m_totalFileSize;
    std::atomic<long long> m_totalDownloadedSize; // Atomic counter for accurate total
    std::vector<std::thread> m_worker_threads;
    std::vector<std::string> m_tempFilePaths; // Store paths for merging/cleanup
    std::atomic<int> m_active_workers; // Count active worker threads
    std::atomic<bool> m_worker_error_occurred; // Flag if any worker failed

    // References to shared data and sync primitives from DownloaderWindow
    std::atomic<bool>& m_cancel_flag_ref;
    Glib::Dispatcher& m_dispatcher_ref;
    DownloaderWindow::UIData& m_ui_data_ref;
    std::mutex& m_ui_data_mutex_ref;

    // --- Libcurl related ---
    // Structure to pass necessary data to static curl callbacks
    struct CurlWorkerData {
        DownloaderLogic* logic_ptr = nullptr;
        std::ofstream* outfile = nullptr;
        long long block_start_pos = 0;
        long long block_current_pos = 0;
        long long block_total_size = 0; // Size of this specific block
    };

    static size_t write_callback_static(char *ptr, size_t size, size_t nmemb, void *userdata);
    static int progress_callback_static(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow);

    // Cleanup temporary files
    void cleanup_temp_files();
};

#endif // DOWNLOADER_LOGIC_H
